package cn.hyp.bantang.module.finance.controller.admin.basegs;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hyp.bantang.framework.common.pojo.PageParam;
import cn.hyp.bantang.framework.common.pojo.PageResult;
import cn.hyp.bantang.framework.common.pojo.CommonResult;
import cn.hyp.bantang.framework.common.util.object.BeanUtils;
import static cn.hyp.bantang.framework.common.pojo.CommonResult.success;

import cn.hyp.bantang.framework.excel.core.util.ExcelUtils;

import cn.hyp.bantang.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hyp.bantang.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hyp.bantang.module.finance.controller.admin.basegs.vo.*;
import cn.hyp.bantang.module.finance.dal.dataobject.basegs.BaseGsDO;
import cn.hyp.bantang.module.finance.service.basegs.BaseGsService;

@Tag(name = "管理后台 - 公司基本信息")
@RestController
@RequestMapping("/finance/base-gs")
@Validated
public class BaseGsController {

    @Resource
    private BaseGsService baseGsService;

    @PostMapping("/create")
    @Operation(summary = "创建公司基本信息")
    @PreAuthorize("@ss.hasPermission('finance:base-gs:create')")
    public CommonResult<String> createBaseGs(@Valid @RequestBody BaseGsSaveReqVO createReqVO) {
        return success(baseGsService.createBaseGs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司基本信息")
    @PreAuthorize("@ss.hasPermission('finance:base-gs:update')")
    public CommonResult<Boolean> updateBaseGs(@Valid @RequestBody BaseGsSaveReqVO updateReqVO) {
        baseGsService.updateBaseGs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('finance:base-gs:delete')")
    public CommonResult<Boolean> deleteBaseGs(@RequestParam("id") String id) {
        baseGsService.deleteBaseGs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('finance:base-gs:query')")
    public CommonResult<BaseGsRespVO> getBaseGs(@RequestParam("id") String id) {
        BaseGsDO baseGs = baseGsService.getBaseGs(id);
        return success(BeanUtils.toBean(baseGs, BaseGsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司基本信息分页")
    @PreAuthorize("@ss.hasPermission('finance:base-gs:query')")
    public CommonResult<PageResult<BaseGsRespVO>> getBaseGsPage(@Valid BaseGsPageReqVO pageReqVO) {
        PageResult<BaseGsDO> pageResult = baseGsService.getBaseGsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseGsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('finance:base-gs:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseGsExcel(@Valid BaseGsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseGsDO> list = baseGsService.getBaseGsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司基本信息.xls", "数据", BaseGsRespVO.class,
                        BeanUtils.toBean(list, BaseGsRespVO.class));
    }

}